const Storage = storage.getGroupStorage<JSONValue>('gameStorage2');
import i18next, { i18n } from 'i18next';
import { StorageAPI, HPAPI, PlayerAPI, itemAPI } from './Extension/classes';
import './Extension/i18n';
console.clear();
console.log(i18next.t('welcome'));
console.log(itemAPI.goComplexList(['hello*4', 'bye*3']));
const 枪 = {
  H416: {
    damage: 130,
    range: 50,
    fireRate: 0.1,
    capacity: 30,
    wearOffset: new GameVector3(0, 0.07, 1),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.RIGHT_HAND,
    bulletCooling: 3,
    bullet: 50,
  },
  M4A1: {
    damage: 128,
    range: 60,
    capacity: 50,
    fireRate: 0.1,
    wearOffset: new GameVector3(0, 0.08, 0.5),
    wearScale: new GameVector3(0.3, 0.3, 0.3),
    wearPart: GameBodyPart.RIGHT_HAND,
    bulletCooling: 2.8,
    bullet: 45,
  },
  AK47: {
    damage: 135,
    range: 40,
    capacity: 30,
    fireRate: 0.1,
    wearOffset: new GameVector3(0, 0.09, 0.6),
    wearScale: new GameVector3(0.4, 0.4, 0.4),
    wearPart: GameBodyPart.RIGHT_HAND,
    bagsCooling: 3.5,
    bulletCooling: 3.5,
    bullet: 42,
  },
  SCAR: {
    damage: 132,
    range: 55,
    capacity: 45,
    fireRate: 0.1,
    wearOffset: new GameVector3(0, 0.07, 1),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.RIGHT_HAND,
    bulletCooling: 3.2,
    bullet: 48,
  },
};
const 刀 = {
  配饰军刀: {
    damage: 10,
    range: 2,
    fireRate: 1,
    wearOffset: new GameVector3(0, 0.07, 1),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.RIGHT_HAND,
  },
};
const 子弹 = {
  一级子弹: {
    damage: 1,
  },
  二级子弹: {
    damage: 2,
  },
  三级子弹: {
    damage: 3,
  },
  四级子弹: {
    damage: 4,
  },
  五级子弹: {
    damage: 5,
  },
  六级子弹: {
    damage: 6,
  },
  七级子弹: {
    damage: 7,
  },
  八级子弹: {
    damage: 8,
  },
  九级子弹: {
    damage: 9,
  },
};
/**
 * 获取枪械的攻击伤害
 */
function getGunhurtNumber(weapon: string, bullet: string, defense: number) {
  const weaponData = 枪[weapon as keyof typeof 枪];
  const bulletData = 子弹[bullet as keyof typeof 子弹];
  if (!weaponData || !bulletData) {
    throw new Error(i18next.t('invalid_weapon_bullet_defense'));
  } else {
    const damage = (weaponData.damage * bulletData.damage) / defense;
    return Math.max(damage, 0);
  }
}
/**
 * 护甲
 */
const 护甲 = {
  无: {
    df: 0,
    wearOffset: new GameVector3(0, 0.07, 1),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.RIGHT_HAND,
  },
  一级护甲: {
    df: 1,
    wearOffset: new GameVector3(0, 0.07, 0),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.TORSO,
  },
  二级护甲: {
    df: 2,
    wearOffset: new GameVector3(0, 0, 0),
    wearScale: new GameVector3(0.6, 0.6, 0.8),
    wearPart: GameBodyPart.TORSO,
  },
  三级护甲: {
    df: 3,
    wearOffset: new GameVector3(0, 0.07, 0),
    wearScale: new GameVector3(0.6, 0.6, 0.6),
    wearPart: GameBodyPart.TORSO,
  },
  四级护甲: {
    df: 4,
    wearOffset: new GameVector3(0, 0.07, 0),
    wearScale: new GameVector3(0.6, 0.6, 0.6),
    wearPart: GameBodyPart.TORSO,
  },
  五级护甲: {
    df: 5,
    wearOffset: new GameVector3(0, -0.07, 0),
    wearScale: new GameVector3(0.5, 0.5, 0.5),
    wearPart: GameBodyPart.TORSO,
  },
  六级护甲: {
    df: 6,
    wearOffset: new GameVector3(0, -0.07, 0),
    wearScale: new GameVector3(0.5, 0.5, 0.5),
    wearPart: GameBodyPart.TORSO,
  },
};
/**
 * 头盔
 */
const 头盔 = {
  无: {
    df: 0,
    wearOffset: new GameVector3(0, 0.07, 1),
    wearScale: new GameVector3(0.75, 0.75, 0.75),
    wearPart: GameBodyPart.RIGHT_HAND,
  },
  一级头盔: {
    df: 1,
    wearOffset: new GameVector3(0, 0.5, 0),
    wearScale: new GameVector3(1.1, 1, 1.1),
    wearPart: GameBodyPart.HEAD,
  },
  二级头盔: {
    df: 2,
    wearOffset: new GameVector3(0, 0.5, 0),
    wearScale: new GameVector3(1.1, 1, 1.1),
    wearPart: GameBodyPart.HEAD,
  },
  三级头盔: {
    df: 3,
    wearOffset: new GameVector3(0, 0.45, 0.05),
    wearScale: new GameVector3(0.65, 0.65, 0.65),
    wearPart: GameBodyPart.HEAD,
  },
  四级头盔: {
    df: 4,
    wearOffset: new GameVector3(0, 0.45, 0.05),
    wearScale: new GameVector3(0.65, 0.65, 0.65),
    wearPart: GameBodyPart.HEAD,
  },
  五级头盔: {
    df: 5,
    wearOffset: new GameVector3(0.03, 0.45, 0),
    wearScale: new GameVector3(0.65, 0.65, 0.65),
    wearPart: GameBodyPart.HEAD,
  },
  六级头盔: {
    df: 6,
    wearOffset: new GameVector3(0.03, 0.45, 0),
    wearScale: new GameVector3(0.65, 0.65, 0.65),
    wearPart: GameBodyPart.HEAD,
  },
};
const 所有护甲 = { ...护甲, ...头盔 };
const 所有装备 = {
  ...枪,
  ...刀,
  ...所有护甲,
};
function listObject(obj: Array<string>) {
  const all: string[] = [];
  for (const i of obj) {
    if (all.includes(i)) {
      all[all.indexOf(i)].split('*')[1] = String(
        Number(all[all.indexOf(i)].split('*')[1]) + 1
      );
    } else {
      all.push(i + '*1');
    }
  }
  return all;
}
const bags = {
  无: {
    size: 0,
    speed: 0,
  },
  轻便旅行者背包: {
    size: 10,
    speed: 0,
  },
  标准旅行者背包: {
    size: 20,
    speed: 0.5,
  },
  高级旅行者背包: {
    size: 30,
    speed: 2,
  },
  超大旅行者背包: {
    size: 40,
    speed: 2.5,
  },
  军用背包: {
    size: 100,
    speed: 3.5,
  },
  高级军用背包: {
    size: 150,
    speed: 5,
  },
  特级军用背包: {
    size: 200,
    speed: 5,
  },
  负重背包: {
    size: 300,
    speed: 7,
  },
};
/**
 * 弹挂
 */
const dg = {
  无: {
    size: 0,
    speed: 0,
  },
  低级弹挂: {
    size: 5,
    speed: 0,
  },
  普通弹挂: {
    size: 10,
    speed: 0.1,
  },
  标准弹挂: {
    size: 15,
    speed: 0.2,
  },
  高级弹挂: {
    size: 20,
    speed: 0.5,
  },
};
const allBags: string[] = [];
for (const i in bags) {
  allBags.push(i);
}
const allDG: string[] = [];
for (const i in dg) {
  allDG.push(i);
}
world.onPlayerJoin(async (event) => {
  const entity = event.entity as GamePlayerEntity;
  entity.player?.forceRespawn();
  await new StorageAPI(entity).loadPlayer();
  entity.player.enableDoubleJump = false;
  entity.bagInformation.size = entity.weapon.length;
  if (entity.useBags === undefined) {
    entity.useBags = '无';
  }
  if (entity.useDG === undefined) {
    entity.useDG = '无';
  }
  if (entity.inGame) {
    entity.player?.directMessage(i18next.t('not_evacuated_lost'));
    entity.useWeapons = [];
    entity.scwzInformation.size = 0;
    entity.inGame = false;
  }
  Object.assign(entity, {
    /**
     * 开镜倍数
     */
    glass: 5,
    /**
     * 射击模式
     */
    shootType: '单发',
    /**
     * 是否自动射击
     */
    autoShoot: false,
    /**
     * 是否自动换弹
     */
    autoReload: false,
    /**
     * 子弹等级
     */
    bulletLevel: 0,
  });
  entity.wearweapons = {
    head: '无',
    torso: '无',
    leftHand: '无',
  };
  new PlayerAPI(entity).wear(
    GameBodyPart.RIGHT_HAND,
    '配饰军刀',
    new GameVector3(0, 0.07, 1),
    new GameVector3(0.75, 0.75, 0.75)
  );
});
world.onPress(async (event) => {
  const entity = event.entity as GamePlayerEntity,
    { button } = event;
  if (button === GameButtonType.CROUCH) {
    // 蹲下时开镜
    if (entity.player.cameraFovY === 0.25) {
      entity.player.cameraFovY = 0.25 - (entity.glass - 1) / 100;
      entity.lastCameraMode = entity.player.cameraMode;
      entity.player.cameraMode = GameCameraMode.FPS;
    } else {
      entity.player.cameraFovY = 0.25;
      entity.player.cameraMode = entity.lastCameraMode;
    }
  }
  if (button === GameButtonType.ACTION1) {
    let options = [
      i18next.t('save_data'),
      i18next.t('warehouse_input_title'),
      i18next.t('field_input_title'),
      i18next.t('safe_input_title'),
      i18next.t('goto_darkzone'),
      '语言设置/Language Settings',
    ];
    if (entity.inGame || entity.hasTag('准备进入暗区')) {
      options = [
        i18next.t('save_data'),
        i18next.t('field_input_title'),
        i18next.t('safe_input_title'),
        i18next.t('settings_title'),
        '语言设置/Language Settings',
      ];
    }
    const bag = await entity.player?.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: `${entity.player?.name}的控制面板`,
      content: i18next.t('control_panel_content', {
        name: entity.player?.name,
        userId: entity.player?.userId,
        bagSize: entity.bagInformation.size,
        bagCapacity: entity.bagInformation.capacity,
        fieldSize: entity.scwzInformation.size,
        fieldCapacity: entity.scwzInformation.capacity,
        money: entity.money,
        useBags: entity.useBags ?? '无',
        useDG: entity.useDG ?? '无',
      }),
      options,
    });
    if (bag) {
      switch (bag.value) {
        case '语言设置/Language Settings':
          const lang = await entity.player?.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: i18next.t('langue_set'),
            content: i18next.t('language_content'),
            options: ['中文', 'English'],
          });
          if (!lang) {
            return;
          }
          await i18next.changeLanguage(lang.value === '中文' ? 'zh' : 'en');
          entity.player?.directMessage(
            i18next.t('language_set', { value: lang.value })
          );
          break;
        case i18next.t('settings_title'):
          const set = await entity.player?.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: i18next.t('settings_title'),
            content: i18next.t('settings_content'),
            options: [
              i18next.t('bullet_level'),
              i18next.t('Automatic_reload'),
              i18next.t('Magnification'),
              i18next.t('shoot_mode'),
              i18next.t('shoot_options'),
            ],
          });
          if (!set) {
            return;
          }
          switch (set.value) {
            case i18next.t('Magnification'):
              const glass = await entity.player?.dialog<GameSelectDialogParams>(
                {
                  type: GameDialogType.SELECT,
                  title: i18next.t('Magnification'),
                  content: i18next.t('scope_content'),
                  options: ['1', '2', '4', '5', '8', '10', '12', '15'],
                }
              );
              if (!glass) {
                return;
              }
              const glassNumber = Number(glass.value);
              if (isNaN(glassNumber) || glassNumber < 1 || glassNumber > 15) {
                entity.player?.directMessage(i18next.t('error_input'));
                return;
              }

              entity.glass = glassNumber;
              entity.player?.directMessage(
                i18next.t('set_scope', { value: glassNumber })
              );
              break;
            case i18next.t('shoot_mode'):
              const shootType =
                await entity.player?.dialog<GameSelectDialogParams>({
                  type: GameDialogType.SELECT,
                  title: i18next.t('shoot_mode'),
                  content: i18next.t('shoot_mode_content'),
                  options: [
                    i18next.t('single_shot'),
                    i18next.t('running_fire'),
                  ],
                });
              if (!shootType) {
                return;
              }
              entity.autoShoot = shootType.value;
              entity.player?.directMessage(
                i18next.t('set_shoot_mode', { value: shootType.value })
              );
              break;
            case i18next.t('shoot_options'):
              const shootOption =
                await entity.player?.dialog<GameSelectDialogParams>({
                  type: GameDialogType.SELECT,
                  title: i18next.t('shoot_option_title'),
                  content: i18next.t('shoot_option_content'),
                  options: [i18next.t('sight_bead')],
                });
              if (!shootOption) {
                return;
              }
              switch (shootOption.value) {
                case i18next.t('sight_bead'):
                  entity.shootType = shootOption.value;
                  entity.player?.directMessage(
                    i18next.t('set_shoot_mode', { value: shootOption.value })
                  );
                  break;
              }
              break;
            case i18next.t('Automatic_reload'):
              const autoReload =
                await entity.player?.dialog<GameSelectDialogParams>({
                  type: GameDialogType.SELECT,
                  title: i18next.t('Automatic_reload'),
                  content: i18next.t('auto_reload_content'),
                  options: [i18next.t('on'), i18next.t('off')],
                });
              if (!autoReload) {
                return;
              }
              switch (autoReload.value) {
                case i18next.t('on'):
                  entity.autoReload = true;
                  entity.player?.directMessage(i18next.t('set_auto_reload_on'));
                  break;
                case i18next.t('off'):
                  entity.autoReload = false;
                  entity.player?.directMessage(
                    i18next.t('set_auto_reload_off')
                  );
                  break;
              }
              break;
            case i18next.t('bullet_level'):
              entity.player?.directMessage(
                i18next.t('bullet_level_deprecated')
              );
              break;
          }
          break;
        case i18next.t('save_data'):
          await new StorageAPI(entity).savePlayer();
          entity.player?.directMessage(i18next.t('save_success'));
          break;

        case i18next.t('warehouse_input_title'):
          const filterWeapon = new Array<string>();
          const eachWeaponNumber = new Array<number>();
          entity.weapon.forEach((x) => {
            if (filterWeapon.includes(x)) {
              eachWeaponNumber[filterWeapon.indexOf(x)]++;
            } else {
              filterWeapon.push(x);
              eachWeaponNumber.push(1);
            }
          });
          const bag1 = await entity.player?.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: i18next.t('warehouse_input_title'),
            content: i18next.t('warehouse_content'),
            options: filterWeapon.map((x, i) => `${x}*${eachWeaponNumber[i]}`),
          });
          if (bag1) {
            const selectValue = bag1.value.split('*')[0];
            const selectIndex = Number(bag1.value.split('*')[1]);
            if (allBags.includes(selectValue)) {
              if (entity.useBags !== '无') {
                entity.weapon.push(entity.useBags);
              }
              entity.useBags = selectValue;
              entity.player?.directMessage(
                i18next.t('use_bag', { bag: selectValue })
              );
              entity.weapon.splice(entity.weapon.indexOf(selectValue), 1);
              entity.scwzInformation.capacity =
                bags[entity.useBags as keyof typeof bags].size +
                dg[entity.useDG as keyof typeof dg].size +
                5;
              return;
            }
            if (allDG.includes(selectValue)) {
              if (entity.useDG !== '无') {
                entity.weapon.push(entity.useDG);
              }
              entity.useDG = selectValue;
              entity.player?.directMessage(
                i18next.t('use_dg', { dg: selectValue })
              );
              entity.weapon.splice(entity.weapon.indexOf(selectValue), 1);
              entity.scwzInformation.capacity =
                bags[entity.useBags as keyof typeof bags].size +
                dg[entity.useDG as keyof typeof dg].size +
                5;
              return;
            }
            const bag2 = await entity.player?.dialog<GameInputDialogParams>({
              type: GameDialogType.INPUT,
              title: i18next.t('warehouse_input_title'),
              content: i18next.t('warehouse_input_content', {
                count: selectIndex,
                value: selectValue,
              }),
            });
            if (bag2) {
              const selectNumber = Number(bag2);
              if (
                isNaN(selectNumber) ||
                selectNumber <= 0 ||
                selectNumber > selectIndex ||
                selectNumber >
                  entity.scwzInformation.capacity - entity.scwzInformation.size
              ) {
                entity.player?.directMessage(i18next.t('input_error'));
                return;
              }
              for (let i = 0; i < selectNumber; i++) {
                entity.weapon.splice(entity.weapon.indexOf(selectValue), 1);
                entity.useWeapons.push(selectValue);
              }
              entity.bagInformation.size -= selectNumber;
              entity.scwzInformation.size += selectNumber;
              entity.player?.directMessage(
                i18next.t('put_in_field', {
                  number: selectNumber,
                  value: selectValue,
                })
              );
            }
          }
          break;
        case i18next.t('field_input_title'):
          const filteruseWeapon = new Array<string>();
          const eachuseWeaponNumber = new Array<number>();
          entity.useWeapons.forEach((x) => {
            if (filteruseWeapon.includes(x)) {
              eachuseWeaponNumber[filteruseWeapon.indexOf(x)]++;
            } else {
              filteruseWeapon.push(x);
              eachuseWeaponNumber.push(1);
            }
          });
          if (entity.hasTag('准备进入暗区') || entity.inGame) {
            const bag4 = await entity.player?.dialog<GameSelectDialogParams>({
              type: GameDialogType.SELECT,
              title: i18next.t('field_input_title'),
              content: i18next.t('enter_darkzone'),
              options: filteruseWeapon.map(
                (x, i) => `${x}*${eachuseWeaponNumber[i]}`
              ),
            });
            if (!bag4) {
              return;
            }
            const selectValue = bag4.value.split('*')[0];
            const selectIndex = Number(bag4.value.split('*')[1]);
            const bag5 = await entity.player?.dialog<GameSelectDialogParams>({
              type: GameDialogType.SELECT,
              title: i18next.t('field_input_title'),
              content: i18next.t('enter_darkzone'),
              options: [
                i18next.t('put_into_safe'),
                i18next.t('wearItem'),
                i18next.t('use_item'),
              ],
            });
            if (!bag5) {
              return;
            }
            switch (bag5.value) {
              case i18next.t('put_in_safe'):
                const bag6 = await entity.player?.dialog<GameInputDialogParams>(
                  {
                    type: GameDialogType.INPUT,
                    title: i18next.t('field_input_title'),
                    content: i18next.t('enter_darkzone', {
                      number: selectIndex,
                      value: selectValue,
                    }),
                  }
                );
                if (bag6) {
                  const selectNumber = Number(bag6);
                  if (
                    isNaN(selectNumber) ||
                    selectNumber <= 0 ||
                    selectNumber > selectIndex ||
                    selectNumber >
                      entity.safeInformation.capacity -
                        entity.safeInformation.size
                  ) {
                    entity.player?.directMessage(i18next.t('input_error'));
                    return;
                  }
                  for (let a = 0; a < selectNumber; a++) {
                    entity.useWeapons.splice(
                      entity.useWeapons.indexOf(selectValue),
                      1
                    );
                    entity.safe.push(selectValue);
                  }
                  entity.safeInformation.size += selectNumber;
                  entity.scwzInformation.size -= selectNumber;
                  entity.player?.directMessage(
                    i18next.t('put_in_safe', {
                      number: selectNumber,
                      value: selectValue,
                    })
                  );
                }
                break;
              case i18next.t('wearItem'):
                if (!Object.keys(所有装备).includes(selectValue)) {
                  entity.player?.directMessage(i18next.t('equip_invalid'));
                  return;
                }
                new PlayerAPI(entity).wear(
                  所有装备[selectValue as keyof typeof 所有装备].wearPart,
                  selectValue,
                  所有装备[selectValue as keyof typeof 所有装备].wearOffset,
                  所有装备[selectValue as keyof typeof 所有装备].wearScale
                );
                if (selectValue in 所有护甲) {
                  if (selectValue in 护甲) {
                    entity.player?.directMessage(
                      i18next.t('wear_armor', {
                        armor: selectValue,
                        df: 所有护甲[selectValue as keyof typeof 所有护甲].df,
                      })
                    );
                    entity.wearweapons.torso = selectValue;
                  }
                  if (selectValue in 头盔) {
                    entity.player?.directMessage(
                      i18next.t('wear_helmet', {
                        helmet: selectValue,
                        df: 所有护甲[selectValue as keyof typeof 所有护甲].df,
                      })
                    );
                    entity.wearweapons.head = selectValue;
                  }
                  /**
                   * 计算防御力
                   */
                  entity.defense =
                    所有护甲[entity.wearweapons.head as keyof typeof 所有护甲]
                      .df +
                    所有护甲[entity.wearweapons.torso as keyof typeof 所有护甲]
                      .df;
                }
                break;
              case i18next.t('use_item'):
                /**
                 * 换子弹有bug
                 */
                if (Object.keys(子弹).includes(selectValue)) {
                  entity.bullet = selectValue;
                } else {
                  return;
                }
                let time =
                  所有装备[entity.wearweapons.leftHand as keyof typeof 枪]
                    .bulletCooling;
                if (!time || isNaN(time)) {
                  entity.player.directMessage(i18next.t('cannot_use_bullet'));
                  return;
                }
                while (time > 0) {
                  await sleep(100);
                  entity.player?.directMessage(
                    i18next.t('bullet_cooling', { value: time.toFixed(1) })
                  );
                  time -= 0.1;
                }
                entity.bulletNumber =
                  所有装备[
                    entity.wearweapons.leftHand as keyof typeof 枪
                  ].bullet;
                break;
            }
            return;
          }
          const bag3 = await entity.player?.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: i18next.t('field_input_title'),
            content: i18next.t('field_content'),
            options: filteruseWeapon.map(
              (x, i) => `${x}*${eachuseWeaponNumber[i]}`
            ),
          });
          if (bag3) {
            const selectValue = bag3.value.split('*')[0];
            const selectIndex = Number(bag3.value.split('*')[1]);
            const bag4 = await entity.player?.dialog<GameInputDialogParams>({
              type: GameDialogType.INPUT,
              title: i18next.t('field_input_title'),
              content: i18next.t('field_input_content', {
                count: selectIndex,
                value: selectValue,
              }),
            });
            if (bag4) {
              const selectNumber = Number(bag4);
              if (
                isNaN(selectNumber) ||
                selectNumber <= 0 ||
                selectNumber > selectIndex ||
                selectNumber >
                  entity.bagInformation.capacity - entity.bagInformation.size
              ) {
                entity.player?.directMessage(i18next.t('input_error'));
                return;
              }
              for (let a = 0; a < selectNumber; a++) {
                entity.useWeapons.splice(
                  entity.useWeapons.indexOf(selectValue),
                  1
                );
                entity.weapon.push(selectValue);
              }
              entity.bagInformation.size += selectNumber;
              entity.scwzInformation.size -= selectNumber;
              entity.player?.directMessage(
                i18next.t('put_in_bag', {
                  number: selectIndex,
                  value: selectValue,
                })
              );
            }
          }
          break;
        case i18next.t('goto_darkzone'):
          entity.player?.directMessage(i18next.t('enter_darkzone'));
          entity.addTag('准备进入暗区'); //玩家并没有加入，不可攻击，但是可以像在暗区里一样穿戴装备，换子弹。
          entity.position.copy(new GameVector3(42, 3, 118.5));
          break;
        case i18next.t('safe_input_title'):
          const bag7 = await entity.player?.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: i18next.t('safe_input_title'),
            content: i18next.t('safe_content'),
            options: listObject(entity.safe),
          });
          if (!bag7) {
            return;
          }
          const selectValue = bag7.value.split('*')[0];
          const selectIndex = Number(bag7.value.split('*')[1]);
          const bag8 = await entity.player?.dialog<GameInputDialogParams>({
            type: GameDialogType.INPUT,
            title: i18next.t('safe_input_title'),
            content: i18next.t('safe_input_content', {
              count: selectIndex,
              value: selectValue,
            }),
          });
          if (bag8) {
            const selectNumber = Number(bag8);
            if (
              isNaN(selectNumber) ||
              selectNumber <= 0 ||
              selectNumber > selectIndex ||
              selectNumber >
                entity.safeInformation.capacity - entity.safeInformation.size
            ) {
              entity.player?.directMessage(i18next.t('error_input'));
              return;
            }
            for (let a = 0; a < selectNumber; a++) {
              entity.safe.splice(entity.safe.indexOf(selectValue), 1);
              entity.useWeapons.push(selectValue);
            }
            entity.safeInformation.size -= selectNumber;
            entity.scwzInformation.size += selectNumber;
            entity.player?.directMessage(
              i18next.t('put_in_field_from_safe', {
                number: selectNumber,
                value: selectValue,
              })
            );
          }
          break;
      }
    }
  }
});
const admins: string[] = ['383021551268490', '302455011206115'];
world.onChat(async (event) => {
  const entity = event.entity as GameEntity;
  if (event.message[0] === '$') {
    if (admins.includes(entity.player?.userId ?? '')) {
      // 处理管理员命令
      try {
        await entity.player?.dialog<GameTextDialogParams>({
          type: GameDialogType.TEXT,
          title: '控制台',
          content: eval(event.message.slice(1)),
        });
      } catch (error) {
        await entity.player?.dialog<GameTextDialogParams>({
          type: GameDialogType.TEXT,
          title: '错误',
          content: error instanceof Error ? error.message : String(error),
        });
      }
    }
  }
});
const joinGame = world.querySelector('#进入暗区') as GameEntity;
joinGame.enableInteract = true;
joinGame.interactHint = i18next.t('goto_darkzone');
joinGame.interactRadius = 5;
joinGame.onInteract(async (event) => {
  const { entity } = event;
  entity.removeTag('准备进入暗区'); //移除准备进入暗区标签，玩家可以被攻击,也可以攻击他人
  world.say(
    i18next.t('player_enter_dark_zone', { player: entity.player?.name })
  );
  const x = Math.random() * 254 + 1;
  const z = Math.random() * 254 + 1;
  let y = 65;
  console.log(voxels.getVoxel(Math.floor(x), Math.floor(y), Math.floor(z)));
  while (voxels.getVoxel(Math.floor(x), Math.floor(y), Math.floor(z)) === 0) {
    y--;
    console.log(y);
  }
  entity.position.copy(new GameVector3(x, y + 3, z));
  entity.inGame = true;
});
